home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / gs262 / gsstate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-30  |  13.3 KB  |  459 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsstate.c */
  20. /* Miscellaneous graphics state operators for Ghostscript library */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "gserrors.h"
  24. #include "gxrefct.h"            /* early for gscie.h */
  25. #include "gxcolor.h"            /* for gscolor2.h, gscie.h */
  26. #include "gzstate.h"
  27. #include "gzdevice.h"
  28. #include "gscspace.h"
  29. #include "gscolor2.h"
  30. #include "gscie.h"
  31. #include "gzcolor.h"            /* requires gxdevice.h */
  32. #include "gzht.h"
  33. #include "gzline.h"
  34. #include "gzpath.h"
  35.  
  36. /* Imported values */
  37. /* The following should include a 'const', but */
  38. /* the Watcom compiler won't accept it. */
  39. extern /*const*/ gx_color_map_procs *cmap_procs_default;
  40.  
  41. /* Forward references */
  42. private gs_state *alloc_gstate(P3(const gs_memory_procs *, const gs_state *,
  43.   const char *));
  44. private int alloc_gstate_contents(P1(gs_state *));
  45. private void free_gstate_contents(P1(gs_state *));
  46. private void copy_gstate_contents(P2(gs_state *pto, const gs_state *pfrom));
  47. #ifdef AMIGA
  48. extern int gx_alloc_ht_cache(gs_state *);
  49. extern void gx_set_black(gs_state *);
  50. extern int gs_initmatrix(gs_state *);
  51. extern int gs_newpath(gs_state *);
  52. extern int gs_initclip(gs_state *);
  53. #endif
  54.  
  55. /* The structure for allocating (most of) the contents of a gstate */
  56. /* all at once. */
  57. typedef struct gs_state_contents_s {
  58.     gx_path path;
  59.     gx_clip_path clip_path;
  60.     line_params line_params;
  61.     halftone_params halftone;
  62.     gs_color_space color_space;
  63.     gs_client_color ccolor;
  64.     gx_device_color dev_color;
  65. } gs_state_contents;
  66. #define gstate_contents(pgs) ((gs_state_contents *)((pgs)->path))
  67.  
  68. /* Allocate and free a structure */
  69. #define alloc_struct(pgs,typ,cname)\
  70.   (typ *)(*(pgs)->memory_procs->alloc)(1, sizeof(typ), cname)
  71. #define free_struct(pgs,ptr,cname)\
  72.   (*(pgs)->memory_procs->free)((char *)(ptr), 1, sizeof(*(ptr)), cname)
  73.  
  74. /* ------ Operations on the entire graphics state ------ */
  75.  
  76. /* Allocate and initialize a graphics state. */
  77. private float
  78. null_transfer(const gs_state *pgs, floatp gray)
  79. #ifndef AMIGA
  80. {    return gray;
  81. #else
  82. {    return (float)gray;
  83. #endif
  84. }
  85. gs_state *
  86. gs_state_alloc(const gs_memory_procs *mprocs)
  87. {    register gs_state *pgs =
  88.         alloc_gstate(mprocs, (gs_state *)0, "gs_state_alloc");
  89.     if ( pgs == 0 ) return 0;
  90.     pgs->saved = 0;
  91.     /* Initialize things not covered by initgraphics */
  92.     gx_path_init(pgs->path, pgs->memory_procs);
  93.     gx_cpath_init(pgs->clip_path, pgs->memory_procs);
  94.     gx_alloc_ht_cache(pgs);
  95.     pgs->halftone->width = pgs->halftone->height =
  96.         pgs->halftone->order_size = 0;
  97.     gs_sethalftonephase(pgs, 0, 0);
  98.     /* Initialize things so that gx_remap_color won't crash. */
  99.     gx_set_black(pgs);
  100.     pgs->overprint = 0;
  101.     pgs->black_generation = 0;
  102.     pgs->undercolor_removal = 0;
  103.     pgs->cmap_procs = cmap_procs_default;
  104.     {    gx_transfer *ptran = &pgs->transfer;
  105.         rc_alloc_struct_n(ptran->gray, gx_transfer_map, mprocs,
  106.                   return 0, "gs_state_alloc", 4);
  107.         ptran->red = ptran->green = ptran->blue = ptran->gray;
  108.         ptran->gray->proc = null_transfer;
  109.         ptran->gray->values[0] = frac_0;
  110.     }
  111.     gs_nulldevice(pgs);
  112.     gs_settransfer(pgs, null_transfer);
  113.     gs_setflat(pgs, 1.0);
  114.     gs_setstrokeadjust(pgs, 1);
  115.     /****** What about the font ? ******/
  116.     pgs->in_cachedevice = pgs->in_charpath = 0;
  117.     pgs->show_gstate = 0;
  118.     pgs->level = 0;
  119.     pgs->fill_adjust = float2fixed(0.25);
  120.     pgs->client_data = 0;
  121.     if ( gs_initgraphics(pgs) < 0 )
  122.        {    /* Something went very wrong */
  123.         return 0;
  124.        }
  125.     return pgs;
  126. }
  127.  
  128. /* Set the client data in a graphics state. */
  129. /* This should only be done to a newly created state. */
  130. void
  131. gs_state_set_client(gs_state *pgs, char/*void*/ *pdata,
  132.   const gs_state_client_procs *pprocs)
  133. {    pgs->client_data = pdata;
  134.     pgs->client_procs = *pprocs;
  135. }
  136.  
  137. /* Get the client data from a graphics state. */
  138. char/*void*/ *
  139. gs_state_client_data(gs_state *pgs)
  140. {    return pgs->client_data;
  141. }
  142.  
  143. /* Free a graphics state */
  144. int
  145. gs_state_free(gs_state *pgs)
  146. {    free_gstate_contents(pgs);
  147.     free_struct(pgs, pgs, "gs_state_free");
  148.     return 0;
  149. }
  150.  
  151. /* Save the graphics state */
  152. int
  153. gs_gsave(gs_state *pgs)
  154. {    gs_state *pnew = alloc_struct(pgs, gs_state, "gs_gsave");
  155.     if ( pnew == 0 )
  156.         return_error(gs_error_VMerror);
  157.     *pnew = *pgs;
  158.     if ( alloc_gstate_contents(pgs) < 0 )
  159.        {    *pgs = *pnew;        /* undo partial alloc */
  160.         free_struct(pgs, pnew, "gs_gsave");
  161.         return_error(gs_error_VMerror);
  162.        }
  163.     copy_gstate_contents(pgs, pnew);
  164.     /* Make pnew, not pgs, have the newly-allocated client data. */
  165.     {    char/*void*/ *pdata = pgs->client_data;
  166.         pgs->client_data = pnew->client_data;
  167.         pnew->client_data = pdata;
  168.     }
  169.     pgs->saved = pnew;
  170.     if ( pgs->show_gstate == pgs )
  171.         pgs->show_gstate = pnew->show_gstate = pnew;
  172.     pgs->level++;
  173.     if_debug2('g', "[g]gsave -> 0x%lx, level = %d\n",
  174.           (ulong)pnew, pgs->level);
  175.     return 0;
  176. }
  177.  
  178. /* Restore the graphics state. */
  179. int
  180. gs_grestore(gs_state *pgs)
  181. {    gs_state *saved = pgs->saved;
  182.     char/*void*/ *pdata = pgs->client_data;
  183.     char/*void*/ *sdata;
  184.     if_debug2('g', "[g]grestore 0x%lx, level was %d\n",
  185.           (ulong)saved, pgs->level);
  186.     if ( !saved ) return gs_gsave(pgs);    /* shouldn't happen */
  187.     sdata = saved->client_data;
  188.     /* Swap back the client data pointers. */
  189.     pgs->client_data = sdata;
  190.     saved->client_data = pdata;
  191.     if ( pdata != 0 && sdata != 0 )
  192.         (*pgs->client_procs.copy)(pdata, sdata);
  193.     free_gstate_contents(pgs);
  194.     *pgs = *saved;
  195.     if ( pgs->show_gstate == saved )
  196.         pgs->show_gstate = pgs;
  197.     free_struct(pgs, saved, "gs_grestore");
  198.     return (pgs->saved == 0 ? gs_gsave(pgs) : 0);
  199. }
  200.  
  201. /* Restore to the bottommost graphics state. */
  202. int
  203. gs_grestoreall(gs_state *pgs)
  204. {    if ( !pgs->saved ) return gs_gsave(pgs);    /* shouldn't happen */
  205.     while ( pgs->saved->saved ) gs_grestore(pgs);
  206.     return gs_grestore(pgs);
  207. }
  208.  
  209. /* Allocate and return a new graphics state. */
  210. gs_state *
  211. gs_gstate(gs_state *pgs)
  212. {    gs_state *pnew = alloc_gstate(pgs->memory_procs, pgs, "gs_gstate");
  213.     if ( pnew == 0 ) return 0;
  214.     copy_gstate_contents(pnew, pgs);
  215.     pnew->saved = 0;
  216.     return pnew;
  217. }
  218.  
  219. /* Copy one previously allocated graphics state to another. */
  220. int
  221. gs_copygstate(gs_state *pto, const gs_state *pfrom)
  222. {    /* This is the same as currentgstate. */
  223.     return gs_currentgstate(pto, pfrom);
  224. }
  225.  
  226. /* Copy the current graphics state to a previously allocated one. */
  227. int
  228. gs_currentgstate(gs_state *pto, const gs_state *pgs)
  229. {    /* We have to copy both the scalar and composite parts */
  230.     /* of the state. */
  231.     gs_state sgs;
  232.     sgs = *pto;
  233.     *pto = *pgs;
  234.     /* Put back the composite part pointers. */
  235. #define gcopy(element)\
  236.     pto->element = sgs.element
  237.     gcopy(path);
  238.     gcopy(clip_path);
  239.     gcopy(line_params);
  240.     gcopy(halftone);
  241.     gcopy(color_space);
  242.     gcopy(ccolor);
  243.     gcopy(dev_color);
  244.     gcopy(cie_render);
  245.     gcopy(transfer.red);
  246.     gcopy(transfer.green);
  247.     gcopy(transfer.blue);
  248.     gcopy(transfer.gray);
  249.     gcopy(device);
  250.     gcopy(client_data);
  251. #undef gcopy
  252.     copy_gstate_contents(pto, pgs);
  253.     return 0;
  254. }
  255.  
  256. /* Restore the current graphics state from a previously allocated one. */
  257. int
  258. gs_setgstate(gs_state *pgs, const gs_state *pfrom)
  259. {    /* The implementation is the same as currentgstate, */
  260.     /* except we must preserve the saved pointer and the level. */
  261.     gs_state *saved = pgs->saved;
  262.     int level = pgs->level;
  263.     int code = gs_currentgstate(pgs, pfrom);
  264.     if ( code < 0 ) return code;
  265.     pgs->saved = saved;
  266.     pgs->level = level;
  267.     return 0;
  268. }
  269.  
  270. /* Swap the saved pointer of the graphics state. */
  271. /* This is provided only for save/restore. */
  272. gs_state *
  273. gs_state_swap_saved(gs_state *pgs, gs_state *new_saved)
  274. {    gs_state *saved = pgs->saved;
  275.     pgs->saved = new_saved;
  276.     return saved;
  277. }
  278.  
  279. /* ------ Operations on components ------ */
  280.  
  281. /* Reset most of the graphics state */
  282. int
  283. gs_initgraphics(register gs_state *pgs)
  284. {    int code;
  285.     gs_initmatrix(pgs);
  286.     if (    (code = gs_newpath(pgs)) < 0 ||
  287.         (code = gs_initclip(pgs)) < 0 ||
  288.         (code = gs_setlinewidth(pgs, 1.0)) < 0 ||
  289.         (code = gs_setlinecap(pgs, gs_cap_butt)) < 0 ||
  290.         (code = gs_setlinejoin(pgs, gs_join_miter)) < 0 ||
  291.         (code = gs_setdash(pgs, (float *)0, 0, 0.0)) < 0 ||
  292.         (code = gs_setgray(pgs, 0.0)) < 0 ||
  293.         (code = gs_setmiterlimit(pgs, 10.0)) < 0
  294.        ) return code;
  295.     return 0;
  296. }
  297.  
  298. /* setflat */
  299. int
  300. gs_setflat(gs_state *pgs, floatp flat)
  301. {    if ( flat <= 0.2 ) flat = 0.2;
  302.     else if ( flat > 100 ) flat = 100;
  303.     pgs->flatness = flat;
  304.     return 0;
  305. }
  306.  
  307. /* currentflat */
  308. float
  309. gs_currentflat(const gs_state *pgs)
  310. {    return pgs->flatness;
  311. }
  312.  
  313. /* setstrokeadjust */
  314. int
  315. gs_setstrokeadjust(gs_state *pgs, int stroke_adjust)
  316. {    pgs->stroke_adjust = stroke_adjust;
  317.     return 0;
  318. }
  319.  
  320. /* currentstrokeadjust */
  321. int
  322. gs_currentstrokeadjust(const gs_state *pgs)
  323. {    return pgs->stroke_adjust;
  324. }
  325.  
  326. /* ------ Internal routines ------ */
  327.  
  328. /* Allocate a graphics state object and its contents, */
  329. /* optionally initializing it from an existing object. */
  330. /* Return 0 if the allocation fails. */
  331. private gs_state *
  332. alloc_gstate(const gs_memory_procs *mprocs, const gs_state *pold,
  333.   const char *cname)
  334. {    gs_state *pgs =
  335.         (gs_state *)(*mprocs->alloc)(1, sizeof(gs_state), cname);
  336.     if ( pgs == 0 ) return 0;
  337.     if ( pold != 0 )
  338.         *pgs = *pold;
  339.     else
  340.         pgs->transfer.red = pgs->transfer.green =
  341.           pgs->transfer.blue = pgs->transfer.gray = 0,
  342.         pgs->cie_render = 0,
  343.         pgs->cie_joint_caches = 0,
  344.         pgs->client_data = 0;
  345.     pgs->memory_procs = mprocs;
  346.     if ( alloc_gstate_contents(pgs) < 0 )
  347.        {    free_struct(pgs, pgs, cname);
  348.         return 0;
  349.        }
  350.     return pgs;
  351. }
  352.  
  353. /* Allocate the contents of a graphics state object. */
  354. /* Return -1 if the allocation fails. */
  355. /* Note that the contents have been smashed in this case. */
  356. private int
  357. alloc_gstate_contents(register gs_state *pgs)
  358. {    gs_proc_alloc_t palloc = pgs->memory_procs->alloc;
  359.     static const char cname[] = "alloc_gstate_contents";
  360.     gs_state_contents *cont =
  361.       (gs_state_contents *)(*palloc)(1, sizeof(gs_state_contents), cname);
  362.     if ( cont == 0 ) return -1;
  363. #define gset(element)\
  364.   pgs->element = &cont->element;
  365.     gset(path);
  366.     gset(clip_path);
  367.     gset(line_params);
  368.     gset(halftone);
  369.     gset(color_space);
  370.     cont->color_space.type = &gs_color_space_type_DeviceGray;    /* for cs_adjust_count */
  371.     gset(ccolor);
  372.     gset(dev_color);
  373. #undef gset
  374. #define galloc(element,type,fail)\
  375.   if ( (pgs->element = (type *)(*palloc)(1, sizeof(type), cname)) == 0 )\
  376.     goto fail
  377. #define gtalloc(element,fail)\
  378.   rc_allocate_struct(pgs->element, gx_transfer_map, pgs->memory_procs,\
  379.           goto fail, cname);
  380.     gtalloc(transfer.red, utr);
  381.     gtalloc(transfer.green, utg);
  382.     gtalloc(transfer.blue, utb);
  383.     gtalloc(transfer.gray, uty);
  384.     galloc(device, device, ud);
  385. #undef galloc
  386. #undef gtalloc
  387.     if ( pgs->client_data != 0 )
  388.     {    if ( (pgs->client_data =
  389.                (*pgs->client_procs.alloc)(pgs->memory_procs)) == 0
  390.            )
  391.             goto ucd;
  392.     }
  393.     rc_increment(pgs->cie_render);
  394.     rc_increment(pgs->cie_joint_caches);
  395.     pgs->device_is_shared = 0;
  396.     return 0;
  397.     /* Undo partial allocations if an allocation failed. */
  398. #define gunalloc(element) free_struct(pgs, pgs->element, cname)
  399. ucd:    gunalloc(device);
  400. ud:    gunalloc(transfer.gray);
  401. uty:    gunalloc(transfer.blue);
  402. utb:    gunalloc(transfer.green);
  403. utg:    gunalloc(transfer.red);
  404. utr:    free_struct(pgs, cont, cname);
  405.     return -1;
  406. #undef gunalloc
  407. }
  408.  
  409. /* Free the contents of a graphics state, but not the state itself. */
  410. private void
  411. free_gstate_contents(gs_state *pgs)
  412. {    gs_proc_free_t pfree = pgs->memory_procs->free;
  413.     static const char cname[] = "free_gstate_contents";
  414.     if ( pgs->client_data != 0 )
  415.         (*pgs->client_procs.free)(pgs->client_data, pgs->memory_procs);
  416. #define gfree(element)\
  417.     (*pfree)((char *)pgs->element, 1, sizeof(*pgs->element), cname)
  418.     gx_cpath_release(pgs->clip_path);
  419.     gx_path_release(pgs->path);
  420.     if ( !pgs->device_is_shared )
  421.         gfree(device);
  422.     rc_decrement(pgs->transfer.gray, pgs->memory_procs, cname);
  423.     rc_decrement(pgs->transfer.blue, pgs->memory_procs, cname);
  424.     rc_decrement(pgs->transfer.green, pgs->memory_procs, cname);
  425.     rc_decrement(pgs->transfer.red, pgs->memory_procs, cname);
  426.     rc_decrement(pgs->cie_render, pgs->memory_procs, cname);
  427.     rc_decrement(pgs->cie_joint_caches, pgs->memory_procs, cname);
  428.     cs_adjust_count(pgs, -1);
  429.     (*pfree)((char *)gstate_contents(pgs), 1, sizeof(gs_state_contents),
  430.          cname);
  431. #undef gfree
  432. }
  433.  
  434. /* Copy the composite parts of a graphics state. */
  435. private void
  436. copy_gstate_contents(gs_state *pto, const gs_state *pfrom)
  437. {    static const char cname[] = "copy_gstate_contents";
  438.     cs_adjust_count(pto, -1);
  439.     *gstate_contents(pto) = *gstate_contents(pfrom);
  440.     cs_adjust_count(pto, 1);
  441. #define gcopy(element)\
  442.     *pto->element = *pfrom->element
  443. #define rccopy(element)\
  444.     rc_assign(pto->element, pfrom->element, pto->memory_procs, cname);
  445.     rccopy(transfer.gray);
  446.     rccopy(transfer.blue);
  447.     rccopy(transfer.green);
  448.     rccopy(transfer.red);
  449.     rccopy(cie_render);
  450.     rccopy(cie_joint_caches);
  451.     gcopy(device);
  452. #undef gcopy
  453. #undef rccopy
  454.     if ( pfrom->client_data != 0 )
  455.         (*pfrom->client_procs.copy)(pto->client_data, pfrom->client_data);
  456.     gx_path_share(pto->path);
  457.     gx_cpath_share(pto->clip_path);
  458. }
  459.